package com.tcu.order.controller;

import com.tcu.exception.SmartException;
import com.tcu.order.entity.ServeType;
import com.tcu.order.service.IServeTypeService;
import com.tcu.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/serveType")
public class ServeTypeController {
    private IServeTypeService typeService;

    @Autowired
    public void setTypeService(IServeTypeService typeService) {
        this.typeService = typeService;
    }

    @PostMapping("add")
    public R add(@RequestBody ServeType serveType){
        if(serveType.getId()!=null){
            throw new SmartException(20001,"id必须为null");
        }
        boolean save = typeService.save(serveType);
        return save ? R.success() : R.error();
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = typeService.removeById(id);
        return b ? R.success() : R.error();
    }

    @PostMapping("update")
    public R update(@RequestBody ServeType serveType){
        if (serveType.getId()==null) {
            throw new SmartException(20001,"id为null，无法更新");
        }
        boolean b = typeService.updateById(serveType);
        return b ? R.success() : R.error();

    }
    @GetMapping("getOne/{id}")
    public R getOne(@PathVariable String id){
        ServeType byId = typeService.getById(id);
        return R.success().data("typeInfo", byId);
    }

    @GetMapping("list")
    public R list(){
        List<ServeType> list = typeService.list();
        return R.success().data("list", list);
    }


}
